﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
// Qt headers
#include <QStackedLayout>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QMouseEvent>
#include <QFrame>
#include <QLabel>

// custom headers
#include "CustomTab.h"
#include "GbmpNew.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 

namespace gmepgcs
{
    CustomTab::CustomTab(QWidget * parent) : QWidget(parent),
        m_headerAlignment(Qt::AlignLeft),
        m_pCurrentHeaderWidget(nullptr),
        m_standAloneHeaderVisible(true)
    {
        Init();
    }

    CustomTab::~CustomTab()
    {
    }

    void CustomTab::AddTabItem(QString header, QWidget * bodyWidget)
    {
        if (!bodyWidget)
        {
            return;
        }
        
        QWidget * pHeaderWidget = CreateHeaderWidget(header);
        if (!pHeaderWidget)
        {
            return;
        }

        pHeaderWidget->setObjectName(header);
        pHeaderWidget->setProperty(HEADER_PROPERTY_NAME, header);
        m_lstHeaderWidgets.push_back(pHeaderWidget);
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pHeaderLayout, L"m_pHeaderLayout为空", L"GMEP", L"2024-01-30");
        m_pHeaderLayout->addWidget(pHeaderWidget);
        UpdateHeaderWidgets();

        m_hsTabItems[pHeaderWidget] = bodyWidget;
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pBodyLayout, L"m_pBodyLayout为空", L"GMEP", L"2024-01-30");
        m_pBodyLayout->addWidget(bodyWidget);        

        if (m_lstHeaderWidgets.count() == 1)
        {
            SwtichToTab(pHeaderWidget);
        }
    }

    void CustomTab::SetStandAloneHeaderVisible(bool visible)
    {
        if (m_standAloneHeaderVisible != visible)
        {
            m_standAloneHeaderVisible = visible;
            UpdateHeaderWidgets();
        }
    }

    void CustomTab::SetTabItemVisible(QString header, bool visible)
    {
        for (auto &headerWidget : m_lstHeaderWidgets)
        {
            if (headerWidget && headerWidget->property("Header") == header)
            {
                if (headerWidget->isVisible() != visible)
                {
                    headerWidget->setVisible(visible);
                    if (headerWidget == m_pCurrentHeaderWidget)
                    {
                        QWidget *pTargetHeaderWidget;
                        for (int32_t i = 0; i != m_lstHeaderWidgets.count(); ++i)
                        {
                            if (m_lstHeaderWidgets[i] && m_lstHeaderWidgets[i]->isVisible())
                            {
                                pTargetHeaderWidget = m_lstHeaderWidgets[i];
                            }
                        }

                        if (pTargetHeaderWidget)
                        {
                            SwtichToTab(pTargetHeaderWidget);
                        }
                    }
                    UpdateHeaderWidgets();
                }

                break;
            }
        }
    }

    Qt::Alignment CustomTab::HeaderAlignment()
    {
        return m_headerAlignment;
    }

    void CustomTab::SetHeaderAlignment(Qt::Alignment alignment)
    {
        if (alignment != m_headerAlignment)
        {
            DBG_WARN_AND_RETURN_VOID_UNLESS(m_pHeaderLayout, L"m_pHeaderLayout为空", L"GMEP", L"2024-01-30");
            m_pHeaderLayout->setAlignment(alignment);
            m_headerAlignment = alignment;
        }
    }

    void CustomTab::SetHeaderBackgorund(QColor & bgColor)
    {
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pHeaderPanelFrame, L"m_pHeaderPanelFrame为空", L"GMEP", L"2024-01-30");
        QPalette palette = m_pHeaderPanelFrame->palette();
        if (palette.color(QPalette::ColorRole::Background) != bgColor)
        {
            palette.setColor(QPalette::ColorRole::Background, bgColor);
            m_pHeaderPanelFrame->setAutoFillBackground(true);
            m_pHeaderPanelFrame->setPalette(palette);
        }
    }

    void CustomTab::SetHeaderVisible(bool visible)
    {
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pHeaderPanelFrame, L"m_pHeaderPanelFrame为空", L"GMEP", L"2024-01-30");
        m_pHeaderPanelFrame->setVisible(visible);
    }

    QWidget * CustomTab::GetCurrentHeaderWidget()
    {
        return m_pCurrentHeaderWidget;
    }

    QWidget * CustomTab::GetWidgetAt(int index)
    {
        return m_lstHeaderWidgets[index];
    }
    void CustomTab::SetShowSplitterLine(bool show)
    {
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pSplitterLine, L"m_pSplitterLine为空", L"GMEP", L"2024-01-30");
        m_pSplitterLine->setVisible(show);
    }

    QString CustomTab::CurrentTab()
    {
        DBG_WARN_AND_RETURN_UNLESS(m_pCurrentHeaderWidget, QString::fromStdWString(L""),L"m_pSplitterLine为空", L"GMEP", L"2024-01-30");
        return m_pCurrentHeaderWidget ? m_pCurrentHeaderWidget->property(HEADER_PROPERTY_NAME).value<QString>() : QString();
    }

    void CustomTab::SwtichToTab(QWidget *headerWidget)
    {
        if (m_lstHeaderWidgets.contains(headerWidget) && m_pCurrentHeaderWidget != headerWidget)
        {
            m_pCurrentHeaderWidget = headerWidget;        
            UpdateHeaderWidgets();
            DBG_WARN_AND_RETURN_VOID_UNLESS(m_pBodyLayout, L"m_pBodyLayout为空", L"GMEP", L"2024-01-30");
            m_pBodyLayout->setCurrentWidget(m_hsTabItems[headerWidget]);
            
            emit CurrentTabChanged(headerWidget != nullptr
                ? headerWidget->property(HEADER_PROPERTY_NAME).value<QString>()
                : QString());
        }
    }

    void CustomTab::showEvent(QShowEvent * event)
    {
        UpdateHeaderWidgets();
    }

    void CustomTab::Init()
    {
        m_pHeaderLayout = NEW_AS_QT(QHBoxLayout);
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pHeaderLayout, L"m_pHeaderLayout为空", L"GMEP", L"2024-01-30");
        m_pHeaderLayout->setSpacing(0);
        m_pHeaderLayout->setContentsMargins(0, 0, 0, 0);
        m_pHeaderLayout->setAlignment(m_headerAlignment);

        m_pHeaderPanelFrame = NEW_AS_QT(QFrame);
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pHeaderPanelFrame, L"m_pHeaderPanelFrame为空", L"GMEP", L"2024-01-30");
        m_pHeaderPanelFrame->setObjectName("HeaderPanelFrame");
        m_pHeaderPanelFrame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
        m_pHeaderPanelFrame->setLayout(m_pHeaderLayout);

        m_pSplitterLine = NEW_AS_QT(QLabel);
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pSplitterLine, L"m_pSplitterLine为空", L"GMEP", L"2024-01-30");
        m_pSplitterLine->setFixedHeight(1);
        m_pSplitterLine->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
        m_pSplitterLine->setContentsMargins(0, 0, 0, 0);
        m_pSplitterLine->setStyleSheet("QLabel{"
            "border:1px solid rgba(189,199,213,255);"
            "}"
        );
        m_pSplitterLine->setVisible(false);

        m_pBodyLayout = NEW_AS_QT(QStackedLayout);
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pBodyLayout, L"m_pBodyLayout为空", L"GMEP", L"2024-01-30");
        m_pBodyLayout->setSpacing(0);
        m_pBodyLayout->setContentsMargins(0, 0, 0, 0);
        m_pBodyLayout->setStackingMode(QStackedLayout::StackOne);
        
        QWidget *pBodyWidget = NEW_AS_QT_CHILD(QWidget, this);
        DBG_WARN_AND_RETURN_VOID_UNLESS(pBodyWidget, L"pBodyWidget为空", L"GMEP", L"2024-01-30");
        pBodyWidget->setObjectName("BodyWidget");
        pBodyWidget->setLayout(m_pBodyLayout);
        pBodyWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);        

        QVBoxLayout *pMainLayout = NEW_AS_QT_CHILD(QVBoxLayout, this);
        DBG_WARN_AND_RETURN_VOID_UNLESS(pMainLayout, L"pMainLayout为空", L"GMEP", L"2024-01-30");
        pMainLayout->setSpacing(0);
        pMainLayout->setContentsMargins(0, 0, 0, 0);
        pMainLayout->addWidget(m_pHeaderPanelFrame);
        pMainLayout->addWidget(m_pSplitterLine);
        pMainLayout->addWidget(pBodyWidget);

        setLayout(pMainLayout);
    }

    void CustomTab::UpdateHeaderWidgets()
    {
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pHeaderPanelFrame, L"m_pHeaderPanelFrame为空", L"GMEP", L"2024-01-30");
        if (!(m_pHeaderPanelFrame->isVisible()))
        {
            m_pHeaderPanelFrame->setVisible(true);
        }

        QList<QWidget*> visibleHeaderWidgets;
        for (int32_t i = 0; i != m_lstHeaderWidgets.count(); ++i)
        {
            if (m_lstHeaderWidgets[i] == nullptr)
            {
                continue;
            }
            if (m_lstHeaderWidgets[i]->isVisible())
            {
                visibleHeaderWidgets.push_back(m_lstHeaderWidgets[i]);
            }
        }

        if (visibleHeaderWidgets.count() <= 1)
        {
            DBG_WARN_AND_RETURN_VOID_UNLESS(m_pHeaderPanelFrame, L"m_pHeaderPanelFrame为空", L"GMEP", L"2024-01-30");
            if (m_pHeaderPanelFrame->isVisible() != m_standAloneHeaderVisible)
            {
                m_pHeaderPanelFrame->setVisible(m_standAloneHeaderVisible);
            }
        }
        else
        {
            DBG_WARN_AND_RETURN_VOID_UNLESS(m_pHeaderPanelFrame, L"m_pHeaderPanelFrame为空", L"GMEP", L"2024-01-30");
            if (!(m_pHeaderPanelFrame->isVisible()))
            {
                m_pHeaderPanelFrame->setVisible(true);
            }
        }

        for (int32_t i=0; i!= visibleHeaderWidgets.count(); ++i)
        {
            auto item = visibleHeaderWidgets[i];
            HeaderStyleFlag currentFlag = item == m_pCurrentHeaderWidget ? HeaderStyleFlag::Current : HeaderStyleFlag::Normal;
            if (i == 0 && i == visibleHeaderWidgets.count() - 1)
            {
                UpdateHeaderWidget(item, HeaderStyleFlag::StandAlone | currentFlag);
            }
            else if (i == 0)
            {
                UpdateHeaderWidget(item, HeaderStyleFlag::First | currentFlag);
            }
            else if (i == visibleHeaderWidgets.count() - 1)
            {
                UpdateHeaderWidget(item, HeaderStyleFlag::Last | currentFlag);
            }
            else
            {
                UpdateHeaderWidget(item, HeaderStyleFlag::Middle | currentFlag);
            }
        }
    }
}
